library(tidyverse)
library(ggthemes)
library(maps)
library(usmap)



city.data <- read.csv('Data/city_data_minimal.csv')

city.data$first.year <- rep(NA,dim(city.data)[1])
city.data$last.year <- rep(NA,dim(city.data)[1])
for(i in 1:nrow(city.data)){
  if(grepl('-',city.data$Range.Provided.By.City[i])){
    city.data$first.year[i] <- unlist(strsplit(as.character(city.data$Range.Provided.By.City[i]),'-',fixed=TRUE))[1]
    city.data$last.year[i] <- unlist(strsplit(as.character(city.data$Range.Provided.By.City[i]),'-',fixed=TRUE))[2]
  }
}



####################################
####################################
##
##    Cities in sample
##
####################################
####################################



coords <- city.data %>%
  filter(Request.Status != 'County Sheriff') %>%
  dplyr::select(lat,lon,fullLoc,Request.Status) %>%
  usmap_transform(input_names = c('lon','lat'))

pdf('FinalFigures/fig2-1.pdf',5,5)
plot_usmap() +
  geom_point(data=coords,aes(x = x, y = y), size = 1.5,
             color = "black", alpha = 0.4) +
  theme(legend.position = "none") 
dev.off()



####################################
####################################
##
##    Time to Data
##
####################################
####################################

pdf('FinalFigures/fig2-2.pdf',4,4)
city.data %>%
  mutate(timeToData = as.numeric(difftime(mdy(city.data$DATE.OF.FINAL.RECEIPT),mdy(DATE.OF.FIRST.REQUEST),units='days'))) %>%
  filter(Request.Status != 'County Sheriff') %>%  
  ggplot(aes(x=timeToData)) +
  geom_histogram() +
  theme_tufte()  +
  theme(axis.line = element_line(color = 'black')) +
  labs(title= 'Time to Provide Police Shooting Records',
       x='Days to Provide Records',y='Total Departments')  +
  scale_y_continuous(expand = c(0,0)) +
  scale_x_continuous(expand = c(0,0))
dev.off()